package cn.itcast.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.util.Map;

// 声明队列有两种方式，bean声明，listener声明

@Component //为了让spring知道有这个类，换句话说就是交给spring管理，转换为bean
public class SpringRabbitListener {

//    @RabbitListener(queues = "simple.queue")
//    public void listenSimpleQueue(String msg){
//         System.out.println("消费者接口到simple.queue的消息:["+msg+"]");
//    }

//    @RabbitListener(queues = "simple.queue")
//    public void listenWorkQueue1(String msg) throws InterruptedException {
//        System.out.println("消费者1接收到simple.queue的消息:["+msg+"]" + LocalTime.now());
//        Thread.sleep(20);
//    }
//
//    @RabbitListener(queues = "simple.queue")
//    public void listenWorkQueue2(String msg) throws InterruptedException {
//        System.err.println("消费者2..........接收到simple.queue的消息:["+msg+"]" + LocalTime.now());
//        Thread.sleep(200);
//    }
//
//    @RabbitListener(queues = "fanout.queue1");
//    public void listenFanoutQueue1(String msg) throws InterruptedException {
//        System.err.println("消费者接收到fanout.queue1的消息:["+msg+"]" + LocalTime.now());
//    }
//    @RabbitListener(queues = "fanout.queue2")
//    public void listenFanoutQueue2(String msg) throws InterruptedException {
//        System.err.println("消费者接收到fanout.queue2的消息:["+msg+"]" + LocalTime.now());
//    }

    //交互机与指定队列绑定
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
            key = {"red","blue"}
    ))
    public void listenDirectQueue1(String msg){
        System.err.println("消费者接收到direct.queue1的消息:["+msg+"]" + LocalTime.now());
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
            key = {"red","yellow"}
    ))
    public void listenDirectQueue2(String msg){
        System.err.println("消费者接收到direct.queue2的消息:["+msg+"]" + LocalTime.now());
    }

    /**
     *   “#”代指0个或多个单词
     *   “*”指表一个单词
     * */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),//队列
            exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),//交换机
            key = "china.#"
    ))
    public void listenTopicQueue1(String msg){
        System.err.println("消费者接收到topic.queue1的消息:["+msg+"]" + LocalTime.now());
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),//队列
            exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),//交换机
            key = "#.news"
    ))
    public void listenTopicQueue2(String msg){
        System.err.println("消费者接收到topic.queue2的消息:["+msg+"]" + LocalTime.now());
    }


    //接收转换为json格式的消息
    @RabbitListener(queues = "object.queue")
    public void listenObjectQueue(Map<String,Object> msg){
        System.err.println("消费者接收到object.queue的消息:"+msg+"" + LocalTime.now());
    }


}
